﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunboxsoft.WinForm;
using Sunboxsoft.ASMS.Client.UI.Properties;
using WebRef = Sunboxsoft.ASMS.Client.Domain.WebRef;
using Service = Sunboxsoft.ASMS.Client.Domain.Service;
using Sunboxsoft.Common;
namespace Sunboxsoft.ASMS.Client.UI.Storage
{
    /// <summary>
    /// 库房管理
    /// </summary>
    public partial class WarehouseListView : UserControl, IView
    {
        //当前页  
        private int _pageIndex = 0;
        //总共几页
        private int _pageCount = 0;

        Promise _promise = new Promise();

        public WarehouseListView()
        {
            InitializeComponent();
            this.GCData.EmbeddedNavigator.ButtonClick += new DevExpress.XtraEditors.NavigatorButtonClickEventHandler(EmbeddedNavigator_ButtonClick);
        }

        #region IView 成员

        public void RegisterAction(ActionNodeCollection collection)
        {
            collection
                .AddAction(new ActionNode("view", Resources.isView, "查看", btnView_Click))
                .AddAction(new ActionNode("create", Resources.isAdd, "新增", btnAdd_Click))
                .AddAction(new ActionNode("edit", Resources.isEdit, "修改", btnEdit_Click))
                .AddAction(new ActionNode("delete", Resources.isDelete, "删除", btnDelete_Click))
                .AddAction(new ActionNode("refresh", Resources.isRefresh, "刷新", btnRefresh_Click));
        }
        public void Active(WinForm.ViewActiveArgs args)
        {
            _promise.Then(() =>
            {
                this.BeginInvoke(new Action(() =>
                {
                    SearchLookUpEditUtil.BindIntCodeTextItem(this.ctlSearchLookUpState,
                       Domain.Constant.CommonStatus.Enumerate(),
                       Domain.Constant.CommonStatus.AllItem,
                       true);
                    _promise.Resolve();
                }));
            })
            .Then(BindData)
            .Run();

        }
        public void Resume(ViewResumeArgs args)
        {
            if (args.ClosedArgs.Result)
            {
                BindData();
            }
        }
        public ViewHeader GetHeader()
        {
            return new ViewHeader("库房管理");
        }
        #endregion

        #region Method
        /// <summary>
        /// 数据绑定
        /// </summary>
        private void BindData()
        {
            Domain.IntCodeTextItem item = SearchLookUpEditUtil.GetSelectedValue<Domain.IntCodeTextItem>(this.ctlSearchLookUpState) as Domain.IntCodeTextItem;
            this.ShowMask("loading...");
            UIContext.Current.InvokeAsync(() =>
            {
                PagedResult<WebRef.VWarehouse> result = new Service.QueryService().QueryWarehousePagedList(
                    this.ctlEditFilter.Text.Trim(),
                    ModuleContext.Current.BusinessId,
                    ModuleContext.Current.StationId,
                    item == null ? Domain.Constant.CommonStatus.ALL : item.Code,
                    _pageIndex * GetPageLimit(),
                    GetPageLimit());
                this.BeginInvoke(new Action(() =>
                {
                    if (result.IsSuccess)
                    {
                        GCData.Paginate<WebRef.VWarehouse>(result, out _pageIndex, out _pageCount);
                        _promise.Resolve();
                    }
                    else
                    {
                        UIContext.Current.ViewManager.ShowError(result.Message);
                        _promise.Reject();
                    }
                    this.HideMask();
                }));
            });
        }
        /// <summary>
        /// 获取每页条数限制
        /// </summary>
        /// <returns></returns>
        private int GetPageLimit()
        {
            int pagesize = 25;
            return pagesize;
        }
        #endregion

        #region Action
        /// <summary>
        /// 查看
        /// </summary>
        private void btnView_Click(object sender, object e)
        {
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VWarehouse item = this.GVData.GetFocusedRow() as WebRef.VWarehouse;
                if (item != null)
                {
                    UIContext.Current.ViewManager.PopupView(new Show.ShowWareHouseDetailView(item), new PopupViewArgs("查看库房详细信息"));
                }
            }
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, object e)
        {
            UIContext.Current.ViewManager.PopupView(new Create.CreateWarehouseView(), new PopupViewArgs("新增仓库"));
        }
        /// <summary>
        /// 修改
        /// </summary>
        private void btnEdit_Click(object sender, object e)
        {
            if (this.GVData.FocusedRowHandle > -1)
            {
                WebRef.VWarehouse item = this.GVData.GetFocusedRow() as WebRef.VWarehouse;
                if (item != null)
                {
                    UIContext.Current.ViewManager.PopupView(new Edit.UpdateWareHouseView(item), new PopupViewArgs("修改库房信息"));
                }
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDelete_Click(object sender, object e)
        {

        }
        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRefresh_Click(object sender, object e)
        {
            _pageIndex = 0;
            BindData();
        }
        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            e.UpdatePageIndex(_pageCount, ref _pageIndex);
            BindData();
        }
        #endregion
    }
}
